package nuonuo.com.view.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import nuonuo.com.view.R;

public class AnimatorActivity extends AppCompatActivity {

    private Button btnAnim;
    private Wrapper mWrap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        btnAnim = (Button) findViewById(R.id.btn_anim);
        mWrap = new Wrapper(btnAnim);
        btnAnim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                performAnim();
            }
        });
    }

    private void performAnim() {
        ObjectAnimator animator = ObjectAnimator.ofInt(mWrap, "width", mWrap.getWidth(),300);
        animator.setDuration(2000);
        animator.setRepeatCount(1);
        animator.start();
    }

    class Wrapper{
        View mTarget;
        public Wrapper(View v){
            mTarget = v;
        }

        public void setWidth(int width){
            mTarget.getLayoutParams().width = width;
            mTarget.requestLayout();
        }

        public int getWidth(){
            return mTarget.getLayoutParams().width;
        }
    }
}
